import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class E3 {

  private static void write(RandomAccessFile raf, 
      String name, long faculty, int course, double score
      ) throws IOException {
    raf.writeUTF(name);
    raf.writeLong(faculty);
    raf.writeInt(course);
    raf.writeDouble(score);
  }

  private static void readAndPrint(RandomAccessFile raf) throws IOException {
    String name = raf.readUTF();
    long faculty = raf.readLong();
    int course = raf.readInt();
    double score = raf.readDouble();
    System.out.format("Read student record:%n name: %s\n faculty number: %d%n course: %d%n score: %f\n", name, faculty, course, score);
  }

  /**
   * @param args
   * @throws FileNotFoundException
   */
  public static void main(String[] args) throws FileNotFoundException {

    RandomAccessFile raf = new RandomAccessFile("students.bin", "rw");
    try {
      write(raf, "Ivo", 23, 1, 2.35);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      try {
        raf.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    raf = new RandomAccessFile("students.bin", "r");
    try {
      while (raf.getFilePointer() < raf.length()) {
        readAndPrint(raf);
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      try {
        raf.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    raf = new RandomAccessFile("students.bin", "rw");
    try {
      raf.seek(raf.length());
      write(raf, "Abadjiev", 55, 3, 5.35);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      try {
        raf.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

  }

}
